﻿using System;
using System.ComponentModel;
using System.Windows.Threading;
using Oxage.Common;
using Oxage.Simulator;
using Oxage.Controls;

namespace Oxage.Models
{
	public class SimulatorWindowViewModel : INotifyPropertyChanged
	{
		#region Constructor
		public SimulatorWindowViewModel()
		{
		}
		#endregion

		#region Public properties
		public IStepDriver Driver
		{
			get
			{
				return this.driver;
			}
			set
			{
				if (this.driver != value)
				{
					this.driver = value;
					OnPropertyChanged("Driver");
				}
			}
		}
		private IStepDriver driver;
		#endregion

		#region Public methods
		public void TestLinearMovement()
		{
			ViewHelper.RunInBackground(() =>
			{
				//Direct driving with fixed 3-axes - easier to understand, useful for scripts and learning (processing)
				//driver.MoveAbs(50, 50, 0);
				//driver.MoveAbs(100, 150, 0);
				//driver.MoveAbs(150, 50, 0);
				//driver.MoveAbs(50, 50, 0);

				//Extensible driving with n-axes
				//var line = new GCodeLineInline();
				//var maker = new LineStepMaker();

				//line.EndPoint = new Point(50, 50);
				//maker.Run(this, line);

				//line.EndPoint = new Point(100, 150);
				//maker.Run(this, line);

				//line.EndPoint = new Point(150, 50);
				//maker.Run(this, line);

				//line.EndPoint = new Point(50, 50);
				//maker.Run(this, line);

				//Try movement with helper
				var helper = new StepMakerHelper(this.Driver);
				helper.MoveAbsolute(50, 50);
				helper.MoveAbsolute(100, 150);
				helper.MoveAbsolute(150, 50);
				helper.MoveAbsolute(50, 50);
			});
		}

		public void TestArcMovement()
		{
			ViewHelper.RunInBackground(() =>
			{
				//Direct driving with fixed 3-axes
				//driver.Arc(200, 360 - 45, 45, true);
				//driver.Arc(100, -360, 360, false);

				//Extensible driving with n-axes
				//var arc = new GCodeArcInline();
				//var maker = new ArcStepMaker();
				//arc.Radius = 200;
				//arc.StartAngle = 360 - 45;
				//arc.EndAngle = 45;
				//arc.Clockwise = true;
				//maker.Run(this, arc);

				//Try arc with helper
				var helper = new StepMakerHelper(this.Driver);
				helper.Arc(200, 360 - 45, 45, true);
			});
		}

		public void TestDiagonal()
		{
			var timer = new DispatcherTimer();
			timer.Interval = TimeSpan.FromMilliseconds(50);
			timer.Tick += (sender, e) =>
			{
				this.Driver.Step(new NPoint(1, 1, 0));
			};
			timer.Start();
		}

		public void TestLine()
		{
			//AddSegment(new Point3D(50, 50, 0));
			var helper = new StepMakerHelper(this.Driver);
			helper.RunGCode(App.ViewModel.Document.Content);
		}

		public void QuickDraw(string gcode)
		{
			Clear();

			var helper = new StepMakerHelper(this.Driver);
			helper.RunGCode(App.ViewModel.Document.Content);
		}

		public void Clear()
		{
			(this.Driver as VectorSimulationCanvas).Clear();
		}
		#endregion

		#region INotifyPropertyChanged Members
		public event PropertyChangedEventHandler PropertyChanged;
		protected virtual void OnPropertyChanged(string propertyName)
		{
			if (PropertyChanged != null)
			{
				PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
			}
		}
		#endregion
	}
}
